<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="关联合同" prop="contractJson">
						<sc-table-select v-model="form.contractJson" :apiObj="apiObjContract" :table-width="400" clearable collapse-tags collapse-tags-tooltip :props="props">
							<template #header="{form, submit}">
								<el-form :inline="true" :model="form">
									<el-form-item>
										<el-input v-model="form.keyword" placeholder="合同名称 / 编号" maxlength="80" clearable></el-input>
									</el-form-item>
									<el-form-item>
										<el-button type="primary" @click="submit">查询</el-button>
									</el-form-item>
								</el-form>
							</template>
							<el-table-column prop="contractCode" label="合同编号" width="100"></el-table-column>
							<el-table-column prop="title" label="合同名称"></el-table-column>
						</sc-table-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="送货单位" prop="trackingComp">
						<el-input v-model="form.trackingComp" placeholder="送货单位" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="送货单号" prop="trackingNumber">
						<el-input v-model="form.trackingNumber" placeholder="送货单号" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="发货人" prop="sendUser">
						<select-user-dialog v-model:accountId="form.sendUser" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="收货人" prop="consignee">
						<el-input v-model="form.consignee" placeholder="收货人" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="收货人电话" prop="iphone">
						<el-input v-model="form.iphone" placeholder="收货人电话" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="发货日期" prop="sendDate">
						<el-date-picker v-model="form.sendDate" placeholder="发货日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="收货地址" prop="address">
						<el-input v-model="form.address" placeholder="收货地址" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="发货备注">
				<el-input v-model="form.remark" placeholder="发货备注" maxlength="380" clearable type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'contract'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from '@/views/module/attachMenu'
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,AttachMenu
	},
	data() {
		return {
			props: {
				label: 'title',
				value: 'contractId',
				keyword: "keyword"
			},
			apiObjContract:this.$API.finance.contract.getContractForSelect,
			apiObj:this.$API.file.attach.uploadFile,
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			fileList:[],
			mode: "add",
			titleMap: {
				add: '录入发货记录',
				edit: '编辑发货记录',
			},
			templates: [],
			form: {
				recordId:"",
				contractId:"",
				contractJson:'',
				trackingComp:'',
				trackingNumber:'',
				sendUser:'',
				consignee: '',
				iphone:'',
				sendDate: '',
				address: '',
				remark: '',
				attachId: ''
			},
			rules: {
				contractJson: [
					{required: true, message: '关联合同不能为空'}
				],
				sendUser:[
					{required: true, message: '发货人不能为空', trigger: 'change'}
				],
				trackingComp:[
					{required: true, message: '送货单位不能为空'}
				],
				iphone:[
					{required: true, message: '收货人电话不能为空'}
				],
				trackingNumber:[
					{required: true, message: '发货单号不能为空'}
				],
				sendDate:[
					{required: true, message: '发货日期不能为空'}
				],
				address:[
					{required: true, message: '发货地址不能为空'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.form.contractId = this.form.contractJson.contractId;
					this.form.contractJson = JSON.stringify(this.form.contractJson);
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.finance.contract.insertContractDelivery.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.finance.contract.updateContractDelivery.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.trackingComp = data.trackingComp
			if(data.contractJson)
			{
				this.form.contractJson = JSON.parse(data.contractJson)
			}
			this.form.trackingNumber = data.trackingNumber
			this.form.sendUser = data.sendUser
			this.form.consignee = data.consignee
			this.form.iphone = data.iphone
			this.form.sendDate = data.sendDate
			this.form.address = data.address
			this.form.remark = data.remark
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
